package com.itwk.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwk.common.utils.PageUtils;
import com.itwk.common.utils.Query;
import com.itwk.gulimall.product.dao.PmsSkuInfoDao;
import com.itwk.gulimall.product.entity.PmsSkuInfoEntity;
import com.itwk.gulimall.product.service.PmsSkuInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;


@Service("pmsSkuInfoService")
public class PmsSkuInfoServiceImpl extends ServiceImpl<PmsSkuInfoDao, PmsSkuInfoEntity> implements PmsSkuInfoService {

    //设置条件检索  page=1&limit=10&key=&catelogId=0&brandId=0&min=0&max=0
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
       String key = (String) params.get("key");
        String catelogId = (String) params.get("catelogId");
        String brandId = (String) params.get("brandId");
        String min = (String) params.get("min");
        String max = (String) params.get("max");
        QueryWrapper<PmsSkuInfoEntity> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(key)){
            queryWrapper.and(wrapper->{
                wrapper.eq("sku_id",key).or().like("sku_name", key).or().like("sku_desc", key);
            });
        }

        if(!StringUtils.isEmpty(catelogId)&&!"0".equals(catelogId)){
            queryWrapper.eq("catalog_id", catelogId);
        }
        if(!StringUtils.isEmpty(brandId)&&!"0".equals(brandId)){
            queryWrapper.eq("brand_id", brandId);
        }
        if(!StringUtils.isEmpty(min)){
            try{
                BigDecimal minPrice = new BigDecimal(min);
                queryWrapper.ge("price", minPrice);

            }catch (Exception e){

            }
        }

        if(!StringUtils.isEmpty(max)){
            try {
                BigDecimal maxPrice = new BigDecimal(max);
                //最大价格必须大于0
                if(maxPrice.compareTo(new BigDecimal("0"))==1){
                    queryWrapper.le("price", maxPrice);
                }


            }catch(Exception e){

            }

        }

        IPage<PmsSkuInfoEntity> iPage = new Query<PmsSkuInfoEntity>().getPage(params);
        IPage<PmsSkuInfoEntity> page = baseMapper.selectPage(iPage, queryWrapper);
        return new PageUtils(page);
    }

}